package com.drought.rained.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.drought.rained.db.DbHelperLJ;

import java.util.ArrayList;

public class LocationDaoLJ {
    public ArrayList<String> getProvince() {
        SQLiteDatabase db = DbHelperLJ.getInstance().getReadableDatabase();
        ArrayList<String> data = new ArrayList<>();
        Cursor cursor = db.rawQuery("SELECT DISTINCT UPPER(Province) FROM location", null);
        while (cursor.moveToNext()) {
            String string = cursor.getString(0);
            data.add(string);
        }
        cursor.close();

        return data;
    }

    public ArrayList<String> getCity(String province) {
        SQLiteDatabase db = DbHelperLJ.getInstance().getReadableDatabase();
        ArrayList<String> data = new ArrayList<>();
        Cursor cursor = db.rawQuery("SELECT DISTINCT UPPER(city) FROM location where province=? COLLATE NOCASE", new String[]{province});
        while (cursor.moveToNext()) {
            String string = cursor.getString(0);
            data.add(string);
        }
        cursor.close();

        return data;
    }

    public ArrayList<String> getTown(String province, String city) {
        SQLiteDatabase db = DbHelperLJ.getInstance().getReadableDatabase();
        ArrayList<String> data = new ArrayList<>();
        Cursor cursor = db.rawQuery("SELECT DISTINCT UPPER(town) FROM location where province=?  COLLATE NOCASE and city=? COLLATE NOCASE", new String[]{province, city});
        while (cursor.moveToNext()) {
            String string = cursor.getString(0);
            data.add(string);
        }
        cursor.close();

        return data;
    }

    public ArrayList<String> getArea(String province, String city, String town) {
        SQLiteDatabase db = DbHelperLJ.getInstance().getReadableDatabase();
        ArrayList<String> data = new ArrayList<>();
        Cursor cursor = db.rawQuery("SELECT DISTINCT UPPER(area) FROM location where province=? COLLATE NOCASE and city=? COLLATE NOCASE and town=? COLLATE NOCASE", new String[]{province, city, town});
        while (cursor.moveToNext()) {
            String string = cursor.getString(0);
            data.add(string);
        }
        cursor.close();

        return data;
    }
}
